package control;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;

import javax.swing.JPanel;

import modle.Circle;
import modle.Line;
import modle.Shape;
import view.DrawPanel;
import view.ToolsPanel;

public class DrawListener implements MouseListener, MouseMotionListener {
	private DrawPanel dPanel;
	private ToolsPanel tPanel;
	private Graphics2D g;
	private ArrayList<Shape> list = new ArrayList<Shape>();
	private int x1, y1, x2, y2;
	private String curTool;
	private Shape curShape;
	
	public DrawListener(DrawPanel dPanel, ToolsPanel tPanel, Graphics2D g) {
		this.dPanel = dPanel;
		this.tPanel = tPanel;
		this.g = g;
		this.list = dPanel.getShapeList();
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		if (list.contains(curShape)) {
			list.remove(curShape);
		}
		switch (curTool) {
		case "Line":
			x2 = e.getX();
			y2 = e.getY();
			curShape = new Line(x1, y1, x2, y2);
			break;
		case "Circle":
			x2 = e.getX();
			y2 = e.getY();
			curShape = new Circle(x1, y1, x2, y2);
		default:
			break;
		}
		list.add(curShape);

		dPanel.repaint();
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		curTool = tPanel.getTool();
		x1 = e.getX();
		y1 = e.getY();
		System.out.println(x1 + "," + y1);
		// drawOnePanel();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		curShape = null;
		System.out.println(x1 + "," + y1);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	private JPanel drawOnePanel() {
		// TODO Auto-generated method stub
		JPanel p = new JPanel();
		dPanel.setLayout(null);
		dPanel.add(p);
		p.setLocation(new Point(x1, y1));
		p.setSize(new Dimension(Math.abs(x1 - x2), Math.abs(y1 - y2)));
		p.setBackground(Color.YELLOW);
		Graphics2D g = (Graphics2D)p.getGraphics();
		g.setColor(Color.RED);
		g.drawLine(0, 0, Math.abs(x1 - x2), Math.abs(y1 - y2));
		return p;
	}
}
